﻿using UnityEngine;
using System.Collections;

public class MovPlayer: MonoBehaviour 
{
	public Texture2D texture_red;
	public Texture2D texture_blue;
	public Texture2D texture_inactive;
	
	public Texture2D texture_red_left;
	public Texture2D texture_blue_left;
	public Texture2D texture_inactive_left;

	public bool left;
	public bool right;
	public AudioClip soundfx;	
	float jumpVelocity = 5.5f;
	public static float movp = 0.1f;
	bool atacando = false;
	bool isGrounded = true;
	public static bool pulo = false;

	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Main._state == 0)
			renderer.material.mainTexture = texture_inactive; 
		
		//if(Main._state == 1)
		//	renderer.material.mainTexture = texture_red; 
		
		//if(Main._state == 2)
		//	renderer.material.mainTexture = texture_blue; 

		if(Input.GetKey(KeyCode.A))
		{
			atacando = true;
		}
		
		else
		{
			atacando = false;	
		}

		if(right)
		{
			if(Main._state == 2)
			{
				renderer.material.mainTexture = texture_red;
			}

			if(Main._state == 1)
			{
				renderer.material.mainTexture = texture_blue;
			}

			if(Main._state == 0)
			{
				renderer.material.mainTexture = texture_inactive;
			}
		}
		else
		{
			if(Main._state == 2)
			{
				renderer.material.mainTexture = texture_red_left;
			}
			
			if(Main._state == 1)
			{
				renderer.material.mainTexture = texture_blue_left;
			}

			if(Main._state == 0)
			{
				renderer.material.mainTexture = texture_inactive_left;
			}
		}
	
		if(Input.GetKey(KeyCode.LeftArrow))
		{
			left = true;
			right = false;
			transform.Translate(-movp, 0, 0);
		}
		else if(Input.GetKey(KeyCode.RightArrow))
		{
			right = true;
			left = false;
			transform.Translate(movp, 0, 0);
		}
		
		if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
		{
			rigidbody.AddForce(0, jumpVelocity, 0, ForceMode.Impulse);
			audio.Play();
			isGrounded = false;
			pulo = true;
		}
		
		if(isGrounded)
		{
			pulo = true;	
		}
		else
		{
			pulo = false;	
		}
		
		isGrounded = Physics.Raycast(transform.position, Vector3.down, 0.5f);
	}
	
	void OnCollisionEnter (Collision other) 
	{
		if(other.gameObject.tag == "Inimigo" && atacando)
		{
			Destroy(other.gameObject);
		}
	}
	
	void OnCollisionStay (Collision other) 
	{		
		if(other.gameObject.tag == "move platform")
		{
			transform.parent = other.transform;
		}
	}
		
	void OnCollisionExit (Collision other) 
	{		
		if(other.gameObject.tag == "move platform")
		{
			transform.parent = null;
		}
	}
	
	/*void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "plataform")
		{
			//transform.parent = other.transform;
			print("PLATFORM!");
		}
	}
	
	void OnTriggerStay(Collider other)
	{
		if(other.gameObject.tag == "plataform")
		{
			transform.parent = other.transform;
			print("ta parenteando");
		}
	}

	void OnTriggerExit(Collider other)
	{
		if(other.gameObject.tag == "plataform")
		{
			print(" FUASUHFASUHFUHA ta parenteando");
			transform.parent = null;

		}
	}*/
	
}
